import *as actionTypes from './constants'

import {getBanners,getHotRecommends,getNewAlbums,getTopList} from '@/service/recommend'


// 修改reducer里的state
 const changeTopBannerAction=(res) => ({
  type:actionTypes.CHANGE_TOP_BANNERS,
  topBanners:res.banners
})

 const changeHotRecommendAction=(res) => ({
  type:actionTypes.CHANGE_HOT_RECOMMEND,
  hotRecommends:res.result
})

 const changeNewAlbumSAction = (res) => ({
  type:actionTypes.CHANGE_NEW_ALBUMS,
  newAlbums:res.albums
})

 const changeUpRankingAction =(res) => ({
  type:actionTypes.CHANGE_UP_RANKING,
  upRanking:res.playlist
})
const changeNewRankingAction =(res) => ({
  type:actionTypes.CHANGE_NEW_RANKING,
  newRanking:res.playlist
})
const changeOriginRankingAction =(res) => ({
  type:actionTypes.CHANGE_ORIGIN_RANKING,
  originRanking:res.playlist
})

// 网络请求部分

export const getTopBannerAction = () => {
  return dispatch => {
    getBanners().then(res => {
      dispatch(changeTopBannerAction(res))
    })
  }
}

export const getHotRecommendAction =  (limit) =>{
  return dispatch => {
    getHotRecommends(limit).then(res => {
      dispatch(changeHotRecommendAction(res))
    })
  }
}

export const getNewAlbumAction = (limit) => {
  return dispatch => {
    getNewAlbums(limit).then(res => {
      dispatch(changeNewAlbumSAction(res))
    })
  }
}

export const getTopListAction = (idx) => {
  return dispatch => {
    getTopList(idx).then(res => {
      switch(idx){
        case 0:
          dispatch(changeUpRankingAction(res))
          break
        case 2:
          dispatch(changeNewRankingAction(res))
          break
        case 3:
          dispatch(changeOriginRankingAction(res))
          break
        default:
      }
    })
  }
}